﻿using server.Model;
using server.Tool;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace server.DAO
{
    public class CommonDao
    {

        protected  string GetSendData(DataResult dataResult,string body)
        {
            string resultJson = Utils.CollectionsConvert.ToJSON(dataResult);
            string sendata = "Post" + Constant.START_SPLIT + body + Constant.END_SPLIT + resultJson + "\r\n";
            return sendata;
        }

        /// <summary>
        /// 通过玩家获取grounp
        /// </summary>
        /// <param name="player"></param>
        /// <returns></returns>
        protected Grounp GetGrounpByPlayer(int player)
        {
            string sql = "select p.* from grounp p join room r on p.id =r.grounpId join room_user ru on r.id = ru.room_id where ru.user_id=" + player;
            List<Grounp> ps = MySqlExecuteTools.GetObjectResult<Grounp>(sql, null);
            if (ps.Count > 0)
            {
                return ps.FirstOrDefault<Grounp>();
            }
            return null;
        }

        /// <summary>
        /// 通过grounpId获取玩家列表
        /// </summary>
        /// <param name="grounpId"></param>
        /// <returns></returns>
        protected List<int> GetUserListByGrounp(int grounpId)
        {
            string sql = "select ru.user_id from grounp p join room r on p.id = r.grounpId join room_user ru on r.id = ru.room_id where p.id = " + grounpId;
            List<object> list = MySqlExecuteTools.GetSingleFieldResult(sql);

            List<int> result = new List<int>();

            list.ForEach((id) =>
            {

                result.Add(int.Parse(id.ToString()));
            });
            return result;
        }

        /// <summary>
        /// 同一个Grounp内 推送数据
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="itemData"></param>
        /// <param name="commandName"></param>
        protected void SendNoticeMessageInGrounp(string userId, object itemData,string commandName)
        {
            Grounp grounp = GetGrounpByPlayer(int.Parse(userId));
            if(grounp==null)
            {
                return;
            }
            int adminUser = grounp.userId;
            List<int> playerUserList = GetUserListByGrounp(grounp.id);
            playerUserList.Add(adminUser);
            ConcurrentDictionary<PubgSession, SessionItem> dic = PubgSession.mOnLineConnections;
            foreach (PubgSession session in dic.Keys)
            {
                SessionItem sessionItem = null;
                dic.TryGetValue(session, out sessionItem);
                foreach (int _userId in playerUserList)
                {
                    if (sessionItem != null && sessionItem.gpsItem != null && sessionItem.gpsItem.userId == _userId)
                    {
                        string resultJson = Utils.CollectionsConvert.ToJSON(itemData);
                        string data = commandName + Constant.START_SPLIT + resultJson + "\r\n";
                        session.Send(data);
                        break;
                    }
                }

            }
        }

        /// <summary>
        /// 通过玩家id获取所在房间的名称
        /// </summary>
        /// <returns></returns>
        protected string GetRoomNameByUerId(string userId)
        {
           string sql = "select r.name from room_user ru  left join room r on r.id = ru.room_id where ru.user_id =  " + userId;
            List<object> roomNameResult = MySqlExecuteTools.GetSingleFieldResult(sql, null);
            return roomNameResult[0].ToString();
        }
 
    }
}
